#include<iostream>
#include<string>
#include<set>
#include<cstring>
#include<vector>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int n;
vector<int>ans;
bool st[N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int i = 1;
	while (!st[i]) {
		st[i] = 1;
		i = a[i];
	}
	int j = i;
	do {
		ans.push_back(j);
		j = a[j];
	} while (i != j);
	cout << ans.size() << endl;
	for (auto x : ans) {
		cout << x << " ";
	}
	return 0;
}